'use strict';

//获取评论接口
module.exports = app => {
    return class HomeworkService extends app.Service {
        constructor(ctx) {
            super(ctx);
        }

        //获取布置作业列表
        async getJobAccounting() {
            let url = this.app.urls('getjobaccounting');
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200) {
                throw new Error(data.message ? data.message : 'error');
            }

            for(let i=0;i<data.data['list'].length;i++){
                data.data['list'][i]['accuracy'] = parseFloat(data.data['list'][i]['accuracy']);
            }


            var date = new Date();

            for(let i=0;i<data.data['list'].length;i++){
                var time = new Date(data.data['list'][i]['createTime']);
                data.data['list'][i]['createMonth'] = time.getMonth()+1;
                if(data.data['list'][i]['createMonth'] == date.getMonth()+1){
                    data.data['list'][i]['createMonth'] = '本月';
                }else{
                    data.data['list'][i]['createMonth'] = time.getMonth()+1+'月';
                }
            }

            var dd= data.data['list'];
            var bb={}
            bb.currentTime="本月";
            bb.data=dd;

            var month=['01','02','03','04','05','06','07','08','09','10','11','12']

            month.reverse();



            var arry = [];

            var currentMonth = date.getMonth()+1;
            for(var j=0;j<month.length;j++){
                var a1 = {};
                a1.currentTime=month[j];
                a1.status = '';
                var a2 = new Array();
                for(var i=0;i<data.data['list'].length;i++){
                    if(data.data['list'][i]['createTime'].substring(5,7) == month[j]){
                        a2.push(data.data['list'][i]);
                        a1.currentTime = data.data['list'][i]['createMonth'];
                        a1.status = 1;
                    }
                }

                a1.data=a2;
                arry.push(a1);
            }

            data.data['list'] = arry;


            console.log(data.data['list'][9]['data']);
            return data.data;
        }

        //获取作业报告
        async getJobDetaile(params) {
            let url = this.app.urls('getjobdetaile');
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    id: params.id,
                }
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200) {
                throw new Error(data.message ? data.message : 'error');
            }
            data.data['accuracy'] = parseFloat(data.data['accuracy']);
            for(let i=0;i<data.data['questionAccuracyDTO'].length;i++){
                data.data['questionAccuracyDTO'][i]['accuracy'] = parseFloat(data.data['questionAccuracyDTO'][i]['accuracy']);
            }
            return data.data;
        }




        //获取作业报告详情内的作业
        async getTeacherQuestion(params) {
            let url = this.app.urls('getTeacherQuestion');
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    id: params.id,
                }
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200) {
                throw new Error(data.message ? data.message : 'error');
            }
            return data.data;
        }

        //题目解析
        async getTeacheranalyze(params) {
            let url = this.app.urls('getteacheranalyze');
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    id: params.id,
                }
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200) {
                throw new Error(data.message ? data.message : 'error');
            }
            return data.data;
        }

        //已完成作业

        async getStudentTask(params){
            let url = this.app.urls('getStudentTask');
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    jobAccountingId: params.id
                }
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200) {
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        //获取已完成作业详情
        async getStudentReport(params){
            let url = this.app.urls('getStudentReport');
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    id: params.id
                }
            });

            let data = result.data;

            if (data.code != 200) {
                throw new Error(data.message ? data.message : 'error');
            }

            data.data['accuracy'] = parseFloat(data.data['accuracy']);

            data.data['time'] =
               this.formatSeconds(parseInt(Date.parse(new Date(data.data['finishDate'])) -
                    Date.parse(new Date(data.data['createDate'])))/1000);


            this.app.logger.info(data.data);
            return data.data;
        }



        //未完成作业
        async unfinished(params) {
            let url = this.app.urls('unfinished');
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data: {
                    jobAccountingId: params.id
                }
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200) {
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }


        /*
        布置作业开始
         */

        //获取教师教的班级
        async getTeacherClazz() {
            let url = this.app.urls('teacherClazz');
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200) {
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        //获取出版社列表
        async getTeacherPress(params) {
            let url = this.app.urls('teacherpress');

            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data:{
                    id:params.id
                }
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200) {
                throw new Error(data.message ? data.message : 'error');
            }

            for(let i =0;i<data.data.length;i++){
                if(data.data[i]['isCheck'] == true){
                    console.log(data.data[i]['id']);
                    return data.data[i];
                }
            }
        }

        //获取题目树
        async getPointList(params){
            let url = this.app.urls('getpointlist');
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data:{
                    pressId:'030d340296774ff4aac6b7fcab2abc0f',
                }
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200) {
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        //根据知识点ID获取题目内容
        async getQuestionlist(params){
            let url = this.app.urls('getquestionlist');
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data:{
                    pointId:params.id,
                }
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200) {
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        //发布作业
        async publishjob(params){
            let url = this.app.urls('publishjob');
            const result = await this.ctx.fetch(url, {
                method: 'post',
                dataType: 'json',
                data:{
                    typeJob:params.typeJob,
                    teacherClazzId:params.teacherClazzId,
                    clazzId:params.clazzId,
                    checkDate:params.checkDate,
                    createDate:params.createDate,
                    teacherQuestionIds:params.teacherQuestionIds,
                    pushStatus:params.pushStatus,
                    name:params.name,
                    studentIds:params.studentIds
                }
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200) {
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }




        //换算时间
        formatSeconds(value) {
            var secondTime = parseInt(value);// 秒
            var minuteTime = 0;// 分
            var hourTime = 0;// 小时
            if(secondTime > 60) {//如果秒数大于60，将秒数转换成整数
                //获取分钟，除以60取整数，得到整数分钟
                minuteTime = parseInt(secondTime / 60);
                //获取秒数，秒数取佘，得到整数秒数
                secondTime = parseInt(secondTime % 60);
                //如果分钟大于60，将分钟转换成小时
                if(minuteTime > 60) {
                    //获取小时，获取分钟除以60，得到整数小时
                    hourTime = parseInt(minuteTime / 60);
                    //获取小时后取佘的分，获取分钟除以60取佘的分
                    minuteTime = parseInt(minuteTime % 60);
                }
            }
            var result = "" + parseInt(secondTime) + "''";

            if(minuteTime > 0) {
                result = "" + parseInt(minuteTime) + "'" + result;
            }
            if(hourTime > 0) {
                result = "" + parseInt(hourTime) + "'" + result;
            }
            return result;
        }
    }

}